/** 
 * ColorSelectorButton.java
 * @author Lex
 * 2007-9-8 ����01:15:35
 */
package org.lex.swing.widgets;

import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.SwingUtilities;

import org.lex.swing.ColorIcon;


public class ColorSelectorButton extends JButton {
	public static final String SELECTED_COLOR_CHANGED_PROPERTY = "selectedColorChangedProperty";
	private static final String TEST_STRING = "Selector";
	private static final String DEFAULT_TITLE = "Color";
	// ===================================================
	private int iconWidth = 1;
	private int iconHeight = 1;
	private Color selectedColor = null;
	private String dialogTitle = DEFAULT_TITLE;

	// =========================
	// Constructors
	// =========================
	public ColorSelectorButton() {
		this(null, DEFAULT_TITLE);
	}

	public ColorSelectorButton(String title) {
		this(null, title);
	}

	public ColorSelectorButton(Color color) {
		this(color, DEFAULT_TITLE);
	}

	public ColorSelectorButton(Color color, String title) {
		super();
		super.setText(null);
		// =============================
		// iconHeight & iconWidth should initialized before setSelectedColor()
		FontMetrics fm = this.getFontMetrics(this.getFont());
		this.iconHeight = fm.getHeight();
		this.iconWidth = fm.stringWidth(TEST_STRING);
		if (null == color)
			color = this.getForeground();
		this.setSelectedColor(color, false);
		this.dialogTitle = title;
		// ==============================
		this.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Color color = JColorChooser.showDialog(root(), dialogTitle, selectedColor);
				if (null == color)
					return;
				setSelectedColor(color);
			}

			private Window root() {
				return SwingUtilities.getWindowAncestor(ColorSelectorButton.this);
			}
		});
	}

	public Color getSelectedColor() {
		return this.selectedColor;
	}

	public void setSelectedColor(Color color) {
		if (null == color)
			return;
		this.setSelectedColor(color, true);
	}

	protected void setSelectedColor(Color color, boolean fireEvent) {
		Color old = this.selectedColor;
		this.selectedColor = color;
		this.setIcon(new ColorIcon(color, iconWidth, iconHeight));
		if (true == fireEvent)
			firePropertyChange(SELECTED_COLOR_CHANGED_PROPERTY, old, color);
	}

	public void addColorChangeListener(PropertyChangeListener l) {
		super.addPropertyChangeListener(SELECTED_COLOR_CHANGED_PROPERTY, l);
	}

	public void removeColorChangeListener(PropertyChangeListener l) {
		super.removePropertyChangeListener(SELECTED_COLOR_CHANGED_PROPERTY, l);
	}

	public void setText(String text) {
		FontMetrics fm = this.getFontMetrics(this.getFont());
		this.iconHeight = fm.getHeight();
		this.iconWidth = fm.stringWidth(text);
		this.setSelectedColor(this.selectedColor, false);
	}
}
